package com.taojin.shoppinginterest.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.taojin.shoppinginterest.BaseApp;
import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.fragment.FavorableFragment;
import com.taojin.shoppinginterest.fragment.FindShopFragment;
import com.taojin.shoppinginterest.fragment.HomeFragment;
import com.taojin.shoppinginterest.fragment.WelfareFragment;
import com.taojin.shoppinginterest.netstate.NetworkStateReceiver;
import com.taojin.shoppinginterest.pop.PopHelper_findShop;
import com.taojin.shoppinginterest.utils.AppManager;
import com.taojin.shoppinginterest.utils.FragmentTabUtils;

import java.util.ArrayList;

@ContentView(R.layout.activity_main)
public class MainActivity extends ActionBarActivity implements FragmentTabUtils.OnRgsExtraCheckedChangedListener{
    @ViewInject(R.id.rg_bottom_home)
    private RadioGroup radioGroup;
    @ViewInject(R.id.rd_home_bottom)
    private RadioButton rbHome;
    @ViewInject(R.id.rd_favorable_bottom)
    private RadioButton rbFavorable;
    @ViewInject(R.id.rd_findshop_bottom)
    private RadioButton rbFind;
    @ViewInject(R.id.rd_welfare_bottom)
    private RadioButton rbWelfare;
    @ViewInject(R.id.rd_mine_bottom)
    private RadioButton rbMine;
    private long firstTime;
    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        AppManager.getAppManager().addActivity(this);
        initView();

    }

    private void initView() {
        fragments=new ArrayList<Fragment>();
        fragments.add(new HomeFragment());
        fragments.add(new FavorableFragment());
        fragments.add(new FindShopFragment());
        fragments.add(new WelfareFragment());
        new FragmentTabUtils(getSupportFragmentManager(), fragments, R.id.framelayout_main, radioGroup).setOnRgsExtraCheckedChangedListener(this);
    }

    @Override
    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {

    }
    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) { //如果两次按键时间间隔大于2秒，则不退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;//更新firstTime
            return;
        } else {
            //两次按键小于2秒时，退出应用
            AppManager.getAppManager().AppExit(BaseApp.getInstance());
            NetworkStateReceiver.unRegisterNetworkStateReceiver(BaseApp.getInstance());
        }
    }


    @OnClick({R.id.rd_mine_bottom,R.id.rd_findshop_bottom})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rd_mine_bottom:
                Intent intent=new Intent(this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.rd_findshop_bottom:
                if (PopHelper_findShop.getInstance().isShowing()) {
                    PopHelper_findShop.getInstance().dismiss();
                } else {
                    PopHelper_findShop.getInstance().init(this, v);
                }
                break;
            default:
                break;
        }

    }
}
